package com.hdoit.kylin.logical.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.Get;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.aliyun.oss.common.utils.DateUtil;
import com.hdoit.kylin.logical.user.dao.IStudentDao;
import com.hdoit.kylin.logical.user.dao.ITeacherStudentDao;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.TeacherStudent;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherStudentService;
import com.hdoit.kylin.logical.viewmodel.user.TeacherStudentSearch;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.message.SearchModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.StringHelper;
/**
* @author luozhicheng
* @email
* @createtime 2017年07月18日 20:47:54
* @comment
*/
@Component
public class StudentServiceImpl extends SuperService<Student> implements IStudentService{
	
	private IStudentDao userDao;
	public IStudentDao getUserDao() {
		return userDao;
	}
	@Resource
	public void setUserDao(IStudentDao userDao) {
		this.userDao = userDao;
	}

	
	private ITeacherStudentService teacherStudentService;
	private IEmployeeService employeeService;
	private IRoleService roleService;
	
	public IEmployeeService getEmployeeService() {
		return employeeService;
	}
	
	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	public IRoleService getRoleService() {
		return roleService;
	}
	
	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	
	public ITeacherStudentService getTeacherStudentService(){
		return teacherStudentService;
	}
	
	@Resource
	public void setTeacherStudentService(ITeacherStudentService teacherStudentService) {
		this.teacherStudentService = teacherStudentService;
	}

	@Override
	public boolean applyChange(Student student) {
		if(employeeService.applyEmployeeLogin(student)){
			if(ModelStatus.add.equals(student.getModelStatus())){
				List<String> roleIdList=roleService.queryListStringByMark("student");
				
				if(!employeeService.applyRoleIds(student, StringHelper.join(roleIdList))){
					throw new DaoBaseRuntimeException("applyRoleIds roll back");
				}
				
				TeacherStudent teacherStudent = new TeacherStudent(student);
				
				if(!teacherStudentService.applyChange(teacherStudent)){
					throw new DaoBaseRuntimeException("add teacher role back");
				}
			}
			
			if(ModelStatus.update.equals(student.getModelStatus())){
				TeacherStudentSearch search = new TeacherStudentSearch();
				search.setStudentId(student.getEmployeeId());
				search.setGraduateMark(YesOrNoInteger.no);
				List<TeacherStudent> teacherStudentList = teacherStudentService.queryList(search,null);
				if(teacherStudentList == null||teacherStudentList.size()<=0){
					TeacherStudent teacherStudent = new TeacherStudent(student);
					teacherStudentService.applyChange(teacherStudent);
				}else if(student.getGraduateMark() == YesOrNoInteger.yes){
					TeacherStudent teacherStudent = teacherStudentList.get(0);
					teacherStudent.setGraduateDate(DateUtils.getCurrent());
					teacherStudent.setGraduateMark(YesOrNoInteger.yes);
					teacherStudent.setModelStatus(ModelStatus.update);
					teacherStudentService.applyChange(teacherStudent);
				}
				
				else if(!teacherStudentList.get(0).getTeacherId().equals(student.getTeacherId())){
					throw new DaoBaseRuntimeException("add teacher role back");
				}
				
			}
			
			if(!super.applyChange(student)){
				throw new DaoBaseRuntimeException("student applyChange roll back");
			}
			
			return true;
		}
		return false;
	}
	
	@Override
	public List<Student> queryListByGroupId(SearchModel search,PagerModel pagerModel,String groupId) {
		if(StringUtils.isEmpty(groupId))
			return queryList(search,pagerModel);
		return userDao.queryListByGroupId(search,pagerModel,groupId);
	}
	@Override
	public String queryGroupIdByLoginId(String loginId) {
		Student student=queryModel(loginId);
		return null==student?null:student.getGroupId();
	}
	@Override
	public int queryCountByGroupId(SearchModel search, String groupId) {
		if(StringUtils.isEmpty(groupId))
			return queryCount(search);
		return userDao.queryCountByGroupId(search, groupId);
	}
	@Override
	public List<Student> queryListByGroupId(String groupId) {
		return userDao.queryListByGroupId(groupId);
	}
}
